package com.cet.algorithm.链表;

/**
 * @program: algorithm
 * @description: 移除链表元素
 * @author: 陈恩涛
 * @create: 2022-03-11 11:45
 **/
public class LC203 {

    public static void main(String[] args) {

    }

    public static ListNode removeElements(ListNode head, int val) {

        // dummy --> head --> ...
        ListNode dummy = new ListNode(-1, head);

        ListNode pre = dummy;

        ListNode current = head;

        while (current != null) {
            if (current.val == val) {
                current = current.next;
                pre.next = current;
            } else {
                current = current.next;
                pre = pre.next;
            }
        }

        return dummy.next;
    }
}
